package com.voltmao.webpro.frame.analyze;

import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.FormViewResult;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.DataUrlNoFoundException;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhouyang on 2018/2/12.
 * 解析类--->处理协议返类
 */
public class DTOSmith {

    private HttpServletRequest request;

    public DTOSmith(HttpServletRequest request) {
        this.request = request;
    }

    public interface ApiLoadBackCall {
        /**
         * 返回数据
         *
         * @param request
         * @param apiResult
         * @return
         * @throws WebProViewException
         */
        Object resultData(HttpServletRequest request, ApiResult apiResult) throws WebProViewException;
    }

    public interface FormLoadBackCall {
        /**
         * 加载表单数据
         * @param request
         * @param apiResult
         * @return
         * @throws WebProViewException
         */
        Object resultFormData(HttpServletRequest request, FormViewResult apiResult) throws WebProViewException;
    }

    public interface LoadGridViewBackCall {
        /**
         * 查询的接口URL
         * @param request
         * @return
         */
        String getDataUrl(HttpServletRequest request);

        /**
         * 返回的列表数据
         * @param request
         * @param apiResult
         * @return
         * @throws WebProViewException
         */
        DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException;

        /**
         * 默认的表单数据
         *
         * @param request
         * @return
         */
        Object defalutForm(HttpServletRequest request) throws WebProViewException;
    }

    public interface SeachGridBackCall {
        /**
         * 返回的列表数据
         *
         * @param request
         * @param apiResult
         * @return
         * @throws WebProViewException
         */
        DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException;
    }

    /**
     * 构造处理类DTO协议对象
     *
     * @param dataLoadBackCall
     * @return
     */
    public ApiResult builderApiResult(ApiLoadBackCall dataLoadBackCall) {
        ApiResult apiResult = new ApiResult();
        try {
            apiResult.status = Constants.SUCCESS.toString();
            apiResult.msg = Constants.SUCCESS.getDes();
            apiResult.data = dataLoadBackCall.resultData(this.request, apiResult);
        } catch (WebProViewException ex) {
            ex.printStackTrace();
            apiResult.status = Constants.FAIVAL.toString();
            apiResult.msg = Constants.FAIVAL.getDes();
        }
        return apiResult;
    }

    /**
     * 构造表单DTO协议对象
     *
     * @param formLoadBackCall
     * @return
     */
    public FormViewResult builderFormViewResult(FormLoadBackCall formLoadBackCall) {
        FormViewResult formViewResult = new FormViewResult();
        try {
            formViewResult.status = Constants.SUCCESS.toString();
            formViewResult.msg = Constants.SUCCESS.getDes();
            formViewResult.form = formLoadBackCall.resultFormData(this.request, formViewResult);

            if (StringUtils.isEmpty(formViewResult.requestUrl)) {
                throw new DataUrlNoFoundException("未定义表单的请求地址,requestUrl为空.");
            }
        } catch (WebProViewException ex) {
            ex.printStackTrace();
            formViewResult.status = Constants.FAIVAL.toString();
            formViewResult.msg = Constants.FAIVAL.getDes();
        } catch (DataUrlNoFoundException ex) {
            ex.printStackTrace();
            formViewResult.status = Constants.AUTH_FAIVAL.toString();
            formViewResult.msg = Constants.AUTH_FAIVAL.getDes();
        }

        return formViewResult;
    }

    /**
     * 构造表格展示页面的DTO协议对象
     *
     * @param gridViewBackCall
     * @return
     */
    public GridViewResult builderLoadGridViewResult(LoadGridViewBackCall gridViewBackCall) {
        GridViewResult result = new GridViewResult();

        try {
            result.status = Constants.SUCCESS.toString();
            result.msg = Constants.SUCCESS.getDes();
            result.dataUrl = gridViewBackCall.getDataUrl(this.request);
            result.data = gridViewBackCall.resultData(this.request, result);
            result.form = gridViewBackCall.defalutForm(this.request);

            if (StringUtils.isEmpty(result.dataUrl)) {
                throw new DataUrlNoFoundException("未定义表格的searchUrl的接口地址.");
            }
        } catch (WebProViewException ex) {
            ex.printStackTrace();
            result.status = Constants.FAIVAL.toString();
            result.msg = Constants.FAIVAL.getDes();
        } catch (DataUrlNoFoundException ex) {
            ex.printStackTrace();
            result.status = Constants.NO_FUN_FAIVAL.toString();
            result.msg = Constants.NO_FUN_FAIVAL.getDes();
        }

        return result;
    }

    /**
     * 构造查询表格的DTO数据
     *
     * @param seachGridBackCall
     * @return
     */
    public GridViewResult builderSeachGridResult(SeachGridBackCall seachGridBackCall) {
        GridViewResult result = new GridViewResult();

        try {
            result.status = Constants.SUCCESS.toString();
            result.msg = Constants.SUCCESS.getDes();
            result.data = seachGridBackCall.resultData(this.request, result);

            if (StringUtils.isEmpty(result.dataUrl)) {
                throw new DataUrlNoFoundException("未定义表格的searchUrl的接口地址.");
            }
        } catch (WebProViewException ex) {
            ex.printStackTrace();
            result.status = Constants.FAIVAL.toString();
            result.msg = Constants.FAIVAL.getDes();
        } catch (DataUrlNoFoundException ex) {
            ex.printStackTrace();
            result.status = Constants.NO_FUN_FAIVAL.toString();
            result.msg = Constants.NO_FUN_FAIVAL.getDes();
        }

        return result;
    }
}